前言:
之前都是一次性訓練好模型,但是一旦資料太多要訓練,動輒就是數小時,甚至幾天
所以我打算改善訓練的過程,讓它能儲存上次進度來訓練模組
程式碼:
import os
import tensorflow as tf
from tensorflow import keras
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.models import Model
from tensorflow.python.keras.layers import Flatten, Dense, Dropout
from tensorflow.python.keras.applications.resnet import ResNet50
from tensorflow.python.keras.optimizer_v2.adam import Adam
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from tensorflow.python.keras.models import Sequential, load_model
from tensorflow.python.keras.layers import LSTM, Dropout, Dense
from tensorflow.python.keras.callbacks import ModelCheckpoint
from tensorflow.python.framework.ops import disable_eager_execution
disable_eager_execution()
tf.compat.v1.experimental.output_all_intermediates(True)
checkpoint_path = "training_1/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
# Create a callback that saves the model's weights
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
save_weights_only=True,
verbose=1)
# 輸出整個網路結構
print(net_final.summary())
# 訓練模型
net_final.fit(train_batches,
steps_per_epoch = train_batches.samples // BATCH_SIZE,
validation_data = valid_batches,
validation_steps = valid_batches.samples // BATCH_SIZE,
epochs = NUM_EPOCHS,
callbacks = [cp_callback])
os.listdir(checkpoint_dir)